package cn.hxzy.controller;


import cn.hxzy.domain.Course;
import cn.hxzy.domain.vo.ChpSecListVo;
import cn.hxzy.domain.vo.CourseListVo;
import cn.hxzy.response.R;
import cn.hxzy.response.StatusEnum;
import cn.hxzy.service.CourseService;
import cn.hxzy.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //获取课程
    @GetMapping("/select")
    public R<List<Course>> select() {
        List<Course> courseList = courseService.selectAll(1);
        return R.success(courseList);
    }

    /**
     * 获取课程列表
     *
     * @param schoolId 学校的编号
     * @return
     */
    @GetMapping("/list/{schoolId}")
    public R<List<CourseListVo>> getCourseList(@PathVariable int schoolId) {

        List<CourseListVo> courseList = courseService.getCourseList(schoolId);
        return R.success(courseList);
    }
    //根据专业方向查课程列表
    @GetMapping("/getCourseListByDirectionId")
    public R<List<CourseListVo>> getCourseListByDirectionId(
            @RequestParam(required = false) Integer directionId,

            @RequestParam(required = false) Integer professionalId,
            @RequestParam(required = false)String courseName
    ) {

        List<CourseListVo> courseList = courseService.getCourseListByDirectionId(directionId,professionalId,courseName);
        if (courseList.size() > 0) {
            return R.success(courseList);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }
    //根据课程名称模糊查询
    @GetMapping("/getCourseListByName")
    public R<List<CourseListVo>> getCourseListByName(@RequestParam String courseName) {
        List<CourseListVo> courseListByName = courseService.getCourseListByName(courseName);
       return R.success(courseListByName);
    }


    /**
     * 根据课程编号获取课程信息
     *
     * @param courseId 课程编号
     * @return
     */
    @GetMapping("/getCourse/{courseId}")
    public R<Course> getCourseById(@PathVariable int courseId) {
        Course course = courseService.getCourseById(courseId);
        if (course != null) {
            return R.success(course);
        }
        return R.error(402, "无数据!");
    }

    /**
     * 添加课程
     *
     * @param course 课程信息
     * @return
     */
    @PostMapping("/save")
    public R saveCourse(@RequestBody Course course) {
        int i = courseService.saveCourse(course);
        if (i == -1) {
            return R.error(411, "该课程已存在!");
        } else if (i == 0) {
            return R.error(410, "新增失败!");
        } else {
            return R.success("新增成功!");
        }
    }

    /**
     * 修改课程信息
     *
     * @param course 课程信息
     * @return
     */
    @PutMapping("/update")
    public R updateCourse(@RequestBody Course course) {
        int i = courseService.updateCourse(course);
        if (i == 0) {
            return R.error(410, "修改失败!");
        } else {
            return R.success("修改成功!");
        }
    }

    /**
     * 删除课程
     *
     * @param courseId 课程编号
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteCourseById(@RequestParam int courseId) {
        boolean b = courseService.removeById(courseId);
        if (b) {
            return R.success("删除成功！");
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 根据班级获取课程
     * @param classId
     * @return
     */
//    @GetMapping("/classCourse")
//    public R classCourse(@RequestParam("classId") int classId) {
//        List<Integer> courseIdByClassId = courseService.getCourseIdByClassId(classId);
//        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("course_id", courseIdByClassId);
//        List<Course> list = courseService.list(queryWrapper);
//        return R.success(list);
//    }


    //获取当前课程的章节小节列表
    @GetMapping("/chpSecList")
    public R<List<ChpSecListVo>> getChpSecList(@RequestParam Integer courseId) {
        List<ChpSecListVo> chpSecList = courseService.ChpSecListVo(courseId);
        return R.success(chpSecList);
    }
}

